<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="psaveing-bottom: 0px;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="keyword" placeholder="关键字查询"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="keywordQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <el-drawer
        title="字典明细!"
        :visible.sync="drawer"
        direction="rtl"
        size="50%">
      <el-col :span="24" class="toolbar" style="psaveing-bottom: 0px;">
        <el-form :inline="true">
          <el-form-item>
            <el-input v-model="keywordDetail" placeholder="关键字查询"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" v-on:click="keywordQueryDetail">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleAddD">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>

      <el-table :data="dictionaryDetails" highlight-current-row v-loading="listLoadingD"  @selection-change="selsChangeD" style="width: 100%;">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="name" label="字典明细名称" >
        </el-table-column>
        <el-table-column prop="dictionaryType.name" label="字典明细类型" >
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template scope="scope">
            <el-button size="small" @click="handleEditD(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDelD(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-col :span="24" class="toolbar">
        <el-button type="danger" @click="batchRemoveD" :disabled="this.selsD.length===0">批量删除</el-button>
        <el-pagination layout="prev, pager, next" @current-change="handleCurrentChangeDetail"
                       :page-size="pageSizeD" :total="totalsD" :current-page="currentPageD"
                       style="float:right;">
        </el-pagination>
      </el-col>

      <el-dialog :title="title" :visible.sync="saveFormVisibleD" :close-on-click-modal="false" append-to-body>
        <el-form :model="saveFormD" label-width="80px" :rules="saveFormRulesD" ref="saveFormD">
          <el-form-item label="字典名称" prop="name">
            <el-input v-model="saveFormD.name" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="字典类型" prop="types_id">
            <el-select v-model="saveFormD.types_id" clearable placeholder="请选择">
              <el-option
                  v-for="item in dictionaryTypes"
                  :label="item.name"
                  :value="item.id">
                <span style="float: left">{{ item.name }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.sn }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="saveFormVisibleD = false">取消</el-button>
          <el-button type="primary" @click.native="saveSubmitD" :loading="saveLoadingD">提交</el-button>
        </div>
      </el-dialog>
    </el-drawer>

    <el-table :data="dictionaryTypes"  @row-dblclick="DictionaryDetail" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection"   width="55" >
      </el-table-column>
      <el-table-column prop="name" label="字典名称"  >
      </el-table-column>
      <el-table-column prop="sn" label="字典编号" >
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :page-size="pageSize" :total="totals" :current-page="currentPage"
                     style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="字典名称" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="字典编号" prop="sn">
          <el-input v-model="saveForm.sn" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
//import util from '../../common/js/util'
//import NProgress from 'nprogress'
//import { getDictionaryTypeListPage, rempveDictionaryType, batchRempveDictionaryType, saveDictionaryType, saveDictionaryType } from '../../api/api';

export default {
  data() {
    return {
      keyword: '', // 关键字查询
      keywordDetail: '',
      title: '',  // 对话框标题
      dictionaryTypes: [],   // 所有员工
      totals: 0,    // 总条数
      totalsD: 0,    // 总条数
      currentPage: 1, // 页数
      currentPageD: 1, // 页数
      dictionaryDetails:[],
      pageSize: 5,    // 页面显示条数
      pageSizeD: 5,    // 页面显示条数
      listLoading: false, // ，忙等
      listLoadingD: false, // ，忙等
      sels: [],//列表选中列
      selsD: [],//列表选中列
      drawer: false,
      dictionaryType: null,
      parentid: null,

      saveFormVisible: false,//编辑界面是否显示
      saveFormVisibleD: false,//编辑界面是否显示
      saveLoading: false,
      saveLoadingD: false,
      saveFormRules: {
        name: [
          {required: true, message: '请输入部门名称', trigger: 'blur'}
        ],
      },
      saveFormRulesD: {
        name: [
          {required: true, message: '请输入部门名称', trigger: 'blur'}
        ],
      },
      //编辑界面数据
      saveForm: {
        id: null,
        sn: "",
        name:'',
      },
      saveFormD: {
        id: null,
        name:'',
        types_id: null,
        dictionaryDetail:null,

      },
    }
  },
  methods: {
    //1.分页查询：获取部门列表
    getDictionaryTypes() {
      //传递的参数
      let para = {
        //分页参数
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        //高级查询参数 - 关键字
        keyword: this.keyword
      };
      //页面一加载就显示加载框
      this.listLoading = true;
      this.$http.post("/DictionaryType",para).then(res => {
        //赋值总数量
        this.totals = res.data.totals;
        //当前页的数据
        this.dictionaryTypes = res.data.data;
        //关闭加载框
        this.listLoading = false;
      }).catch(res => {
        this.$message.error('系统繁忙，请稍后重试!!!');
      })
    },
    getDictionaryDetails() {
      //传递的参数
      let para = {
        //分页参数
        currentPage: this.currentPageD,
        parentid: this.parentid,
        pageSize: this.pageSizeD,
        //高级查询参数 - 关键字
        keywordDetail: this.keywordDetail
      };
      //页面一加载就显示加载框
      this.listLoadingD = true;
      this.$http.post("/DictionaryDetail",para).then(res => {
        //赋值总数量
        this.totalsD = res.data.totals;
        //当前页的数据
        this.dictionaryDetails = res.data.data;
        //关闭加载框
        this.listLoadingD = false;
      }).catch(res => {
        this.$message.error('系统繁忙，请稍后重试!!!');
      })
    },

    //2.点击某一页触发的方法
    handleCurrentChange(val) {
      //当前页
      this.currentPage = val;
      this.getDictionaryTypes();
    },
    handleCurrentChangeDetail(val) {
      //当前页
      this.currentPageD = val;
      this.getDictionaryDetails();
    },

    //3.关键字查询
    keywordQuery(){
      //关键字查询从第一页开始显示
      this.currentPage = 1;
      this.getDictionaryTypes();
    },
    //3.关键字查询
    keywordQueryDetail(){
      //关键字查询从第一页开始显示
      this.currentPage = 1;
      this.getDictionaryDetails();
    },

    //4.删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '温馨提示', {
        type: 'warning'
      }).then(() => { //点击确认之后要执行的代码
        //显示加载框
        this.listLoading = true;
        //获取id
        var id = row.id;
        this.$http.delete("/DictionaryType/"+id).then(res => {
          if(res.data.success){//删除成功
            this.$message.success('删除成功'); //3s自动消失
            //如果时最后一页并且时最后一条数据的时候：当前页 = 当前页-1
            //最后一条数据：(this.totals - 1)%this.pageSize == 0
            //总页数：Math.ceil(this.totals/this.pageSize)
            var totalPage = Math.ceil(this.totals/this.pageSize);
            if(totalPage==this.currentPage && this.currentPage>1 && (this.totals - 1)%this.pageSize == 0){
              this.currentPage = this.currentPage - 1;
            }

            this.getDictionaryTypes();
            this.getDictionaryDetails();
          }else{
            this.$message.error('系统繁忙，请稍后重试【500】');
          }
          this.listLoading = false;//关闭加载框
        }).catch(res => {
          this.$message.error('系统繁忙，请稍后重试【400,404】');
        })
      }).catch(() => { //点击取消之后要执行的代码
      });
    },
    handleDelD: function (index, row) {
      this.$confirm('确认删除该记录吗?', '温馨提示', {
        type: 'warning'
      }).then(() => { //点击确认之后要执行的代码
        //显示加载框
        this.listLoadingD = true;
        //获取id
        var id = row.id;
        this.$http.delete("/DictionaryDetail/"+id).then(res => {
          if(res.data.success){//删除成功
            this.$message.success('删除成功'); //3s自动消失
            //如果时最后一页并且时最后一条数据的时候：当前页 = 当前页-1
            //最后一条数据：(this.totals - 1)%this.pageSize == 0
            //总页数：Math.ceil(this.totals/this.pageSize)
            var totalPage = Math.ceil(this.totalsD/this.pageSizeD);
            if(totalPage==this.currentPageD && this.currentPageD>1 && (this.totalsD - 1)%this.pageSizeD == 0){
              this.currentPageD = this.currentPageD - 1;
            }
            this.getDictionaryDetails();
          }else{
            this.$message.error('系统繁忙，请稍后重试【500】');
          }
          this.listLoadingD = false;//关闭加载框
        }).catch(res => {
          this.$message.error('系统繁忙，请稍后重试【400,404】');
        })
      }).catch(() => { //点击取消之后要执行的代码
      });
    },

    //5.选项改变的时候触发
    selsChange: function (sels) {
      //获取被选中的对象  - 集合
      this.sels = sels;
    },
    selsChangeD: function (sels) {
      //获取被选中的对象  - 集合
      this.selsD = sels;
    },

    //6.批量删除
    batchRemove: function () {
      //获取被选中对象的id们
      var ids = this.sels.map(item => item.id); //[1,2,3]

      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/DictionaryType",ids).then(res => {
          if(res.data.success){//删除成功
            this.$message.success('删除成功'); //3s自动消失
            //如果时最后一页并且时最后一条数据的时候：当前页 = 当前页-1
            //最后一条数据：(this.totals - 1)%this.pageSize == 0
            //总页数：Math.ceil(this.totals/this.pageSize)
            alert(ids.length)
            var totalPage = Math.ceil(this.totals/this.pageSize);
            if(totalPage==this.currentPage && this.currentPage>1 && (this.totals - 1)%this.pageSize == 0){
              this.currentPage = this.currentPage - 1;
            }
0
            this.getDictionaryTypes();
            this.getDictionaryDetails();
          }else{
            this.$message.error('系统繁忙，请稍后重试【500】');
          }
          this.listLoading = false;//关闭加载框
        }).catch(res => {
          this.$message.error('系统繁忙，请稍后重试【400,404】');
        })
      }).catch(() => {
      });
    },//6.批量删除
    batchRemoveD: function () {
      //获取被选中对象的id们
      var ids = this.selsD.map(item => item.id); //[1,2,3]
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoadingD = true;
        this.$http.patch("/DictionaryDetail",ids).then(res => {
          if(res.data.success){//删除成功
            this.$message.success('删除成功'); //3s自动消失
            //如果时最后一页并且时最后一条数据的时候：当前页 = 当前页-1
            //最后一条数据：(this.totals - 1)%this.pageSize == 0
            //总页数：Math.ceil(this.totals/this.pageSize)
            var totalPage = Math.ceil(this.totalsD/this.pageSizeD);
            if(totalPage==this.currentPageD && this.currentPageD>1 && (this.totalsD - 1)%this.pageSizeD == 0){
              this.currentPageD = this.currentPageD - 1;
            }
            this.getDictionaryDetails();
          }else{
            this.$message.error('系统繁忙，请稍后重试【500】');
          }
          this.listLoadingD = false;//关闭加载框
        }).catch(res => {
          this.$message.error('系统繁忙，请稍后重试【400,404】');
        })
      }).catch(() => {
      });
    },


    //7.点击添加按钮弹出对话框
    handleAdd: function () {
      this.saveForm = {
        id: null,
        sn: "",
        name:'',
      };
      this.title = " 添 加 ";
      this.saveFormVisible = true;
    },
    handleAddD: function () {
      this.saveFormD = {
        id: null,
        name:'',
        types_id:null,
      };
      this.title = " 添 加 ";
      this.saveFormVisibleD = true;
    },

    //8.点击编辑按钮弹出对话框
    handleEdit: function (index, row) {
      //克隆：防止对象引用修改显示的数据
      this.saveForm = Object.assign({}, row);

      this.title = " 修 改 ";
      //显示对话框
      this.saveFormVisible = true;
    },
    handleEditD: function (index, row) {
      //克隆：防止对象引用修改显示的数据
      this.saveFormD = Object.assign({}, row);

      this.title = " 修 改 ";
      //显示对话框
      this.saveFormVisibleD = true;
    },

    //9.点击提交按钮添加或修改数据
    saveSubmit: function () {
      //所有对话框中  一个名称为 saveForm的对话框  验证【上面的验证规则通过了 - 返回true，否则返回false】
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading = true;
            let para = Object.assign({}, this.saveForm);
            this.$http.put("/DictionaryType",para).then(res => {
              if(res.data.success){
                this.$message.success("操作成功");
                this.getDictionaryTypes();
              }else{
                this.$message.error("系统繁忙，请稍后重试【500】")
              }
              this.saveLoading = false;
              this.saveFormVisible = false;
            }).catch(res => {
              this.$message.error("系统繁忙，请稍后重试【400,404】")
            })
          });
        }
      });
    },

    //9.点击提交按钮添加或修改数据
    saveSubmitD: function () {
      //所有对话框中  一个名称为 saveForm的对话框  验证【上面的验证规则通过了 - 返回true，否则返回false】
      this.$refs.saveFormD.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoadingD = true;
            let para = Object.assign({}, this.saveFormD);
            console.log(para)
            this.$http.put("/DictionaryDetail",para).then(res => {
              if(res.data.success){
                this.$message.success("操作成功");
                this.getDictionaryDetails();
              }else{
                this.$message.error("系统繁忙，请稍后重试【500】")
              }
              this.saveLoadingD = false;
              this.saveFormVisibleD = false;
            }).catch(res => {
              this.$message.error("系统繁忙，请稍后重试【400,404】")
            })
          });
        }
      });
    },
    DictionaryDetail:function (row){
      this.parentid = row .id,
      this.drawer = true;
      this.getDictionaryDetails(row);
    },
  },
  mounted() {
    // 页面一加载就查询所有的emp
    this.getDictionaryTypes();
    this. getDictionaryDetails();
  }
}
</script>

<style scoped>

</style>